package com.x.wx.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.x.security.model.Resource;
import com.x.wx.model.WxAccount;
import com.x.wx.rpc.WxAccountRpcService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.User;
import com.x.erp.rpc.UserRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.security.model.Account;
import com.x.security.model.SecurityAccount;
import com.x.security.rpc.SecurityRpcService;
import com.x.security.util.Constant;

@RestController
public class UserController extends BaseController{
	private static ModelAndView loginModelAndView = new ModelAndView(Constant.redirect + "/index.jsp");

	@Reference
	private UserRpcService userRpcService;

	@Reference
	private WxAccountRpcService wxAccountRpcService;

	@RequestMapping("/getUserSession")
	public ModelAndView login(HttpServletRequest request) {
		HttpSession session = request.getSession();
		SecurityAccount account = (SecurityAccount) session.getAttribute(Constant.SECURITY_ACCOUNT);
		String accountId = account.getAccountId();
		List<Resource> resourceList = account.getResourceList();
		List<Map<String, Object>> menuList = new ArrayList<>();
		Map<String, Object> res = null;
		for(Resource resource : resourceList) {
			List<Map<String, Object>> menuList1 = new ArrayList<>();
			res = new HashMap<>();
			res.put("menuName", resource.getResourceName());
			res.put("icon", resource.getResourceIcon());
			res.put("menuSeq", resource.getResourceSeq());
			if (resource.getResourceList().size() > 0) {
				for (Resource resource1 : resource.getResourceList()) {
					Map<String, Object> resSecond = new HashMap<>();
					resSecond.put("menuName", resource1.getResourceName());
					resSecond.put("menuUrl", resource1.getResourceUrl());
					resSecond.put("menuSeq", resource1.getResourceSeq());
					menuList1.add(resSecond);
				}
				res.put("menuSecondList", menuList1);
			}
			menuList.add(res);
		}
		User user = new User();
		user.setAccountId(accountId);
		List<User> userList = userRpcService.selectUserList(user);
		if (userList.size() > 0) {
			user = userList.get(0);
			session.setAttribute("userId", user.getUserId());
			session.setAttribute("userName", user.getUserName());
			session.setAttribute("userMobile", user.getUserMobile());
			session.setAttribute("userType", user.getUserType());
			session.setAttribute("userState", user.getUserState());
			if(user.getMarket()!=null && user.getMarket().getMarketId() != null) {
				WxAccount wxAccount = new WxAccount();
				wxAccount.setWxAccountState(1);
				wxAccount.setMarketId(user.getMarket().getMarketId());
				List<WxAccount>  wxAccountList = wxAccountRpcService.selectWxAccountList(wxAccount);
				if (wxAccountList != null && !wxAccountList.isEmpty()){
					session.setAttribute("wxAccountId", wxAccountList.get(0).getWxAccountId());
					session.setAttribute("wxAppId", wxAccountList.get(0).getWxAppId());
				}
				session.setAttribute("marketId", user.getMarketId());
			}
			if(user.getShopId() != null) {
				session.setAttribute("shopId", user.getShopId());
			}
		}
		session.setAttribute("wxMenu", JSON.toJSONString(menuList));
		return loginModelAndView;
	}
}
